package com.gitee.wsl.compose.animation

import androidx.compose.animation.core.AnimationVector
import androidx.compose.animation.core.AnimationVector1D
import androidx.compose.animation.core.AnimationVector2D
import androidx.compose.animation.core.TwoWayConverter
import androidx.compose.animation.core.VectorConverter
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType

//val A.Converter.Float: TwoWayConverter<Float, AnimationVector1D> get() = Float.VectorConverter
//val A.Converter.Int: TwoWayConverter<Int, AnimationVector1D> get() = Int.VectorConverter
//val A.Converter.TextUnitConverter get() = TextUnit.VectorConverter
//val A.Converter.RectConverter: TwoWayConverter<Rect, AnimationVector1D>  get() = Rect.VectorConverter
//val A.Converter.DpConverter get() = Dp.VectorConverter
//val A.Converter.DpOffsetConverter get() = DpOffset.VectorConverter
//val A.Converter.SizeConverter get() = Size.VectorConverter
//val A.Converter.OffsetConverter get() = Offset.VectorConverter
//val A.Converter.IntOffsetConverter get() = IntOffset.VectorConverter
//val A.Converter.IntSizeConverter get() = IntSize.VectorConverter


fun <T, V : AnimationVector> A.Converter(
    convertToVector: (T) -> V,
    convertFromVector: (V) -> T
) = TwoWayConverter(convertToVector,convertFromVector)


val TextUnit.Companion.VectorConverter get() = TextUnitConverter

object TextUnitConverter : TwoWayConverter<TextUnit, AnimationVector2D> {
    override val convertFromVector: (AnimationVector2D) -> TextUnit
        get() = {
            TextUnit(
                it.v1,
                if (it.v2 > 0.5f) TextUnitType.Em else TextUnitType.Sp
            )
        }
    override val convertToVector: (TextUnit) -> AnimationVector2D
        get() = {
            AnimationVector(
                it.value,
                if (it.isEm) 1f else 0f
            )
        }

}